﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Wormhole.Engine.Communication.Messages
{
	/// <author>Hannes Foulds</author>
	/// <date>26 September 2008</date>
	/// <summary>
	/// This class is used for messegas relating to card actions.
	/// </summary>
	public class CardActionMessage : Message
	{
		#region Properties

		#region Card Action
		/// <summary>
		/// The card action the message is for.
		/// </summary>
		public Action.CardAction CardAction { get; set; }
		#endregion

		#endregion

		#region Constructor
		/// <summary>
		/// Initialze a new instance of the class.
		/// </summary>
		/// <param name="cardAction">The card action</param>
		public CardActionMessage(Action.CardAction cardAction)
		{
			this.Action = Actions.CardAction;
			this.CardAction = cardAction;
		}

		/// <summary>
		/// Initailize a new instance of the class from its string representation.
		/// </summary>
		/// <param name="message">The string message.</param>
		public CardActionMessage(string message)
		{
			this.Action = Actions.CardAction;
			this.Parse(message);
		}
		#endregion

		#region To String
		/// <summary>
		/// Convert the message to a string that can be send via the communications channel.
		/// </summary>
		/// <returns>Returns the string representation of the message.</returns>
		public override string ToString()
		{
			return string.Format("{0}|{1}|{2}|{3}", 
				EnumConvertor<Actions>.ToString(this.Action), 
				this.CardAction.CardID, 
				EnumConvertor<Wormhole.Engine.Communication.Action.CardActionType>.ToString(this.CardAction.Action), 
				this.CardAction.TargetID);
		}
		#endregion

		#region Parse
		/// <summary>
		/// Parse a received game list message.
		/// </summary>
		/// <param name="message">The string message to parse.</param>
		protected void Parse(string message)
		{
			string[] messageParts = message.Split('|');

			// create the card action
			this.CardAction = new Wormhole.Engine.Communication.Action.CardAction();
			this.CardAction.CardID = Convert.ToInt32(messageParts[1]);
			this.CardAction.Action = EnumConvertor<Wormhole.Engine.Communication.Action.CardActionType>.FromString(messageParts[2]);
			this.CardAction.TargetID = Convert.ToInt32(messageParts[3]);
		}
		#endregion
	}
}
